package com.huizhan.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.web.servlet.config.annotation.*;
import springfox.documentation.builders.*;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; 

@Configuration
@EnableSwagger2
public class WebConfig extends WebMvcConfigurationSupport{
        
        @Value("${uploadFile.resourceHandler}")
        private String resourceHandler;//请求 url 中的资源映射，不推荐写死在代码中，最好提供可配置，如 /uploadFiles/**
        
        @Value("${uploadFile.location}")
        private String location;//上传文件保存的本地目录，使用@Value获取全局配置文件中配置的属性值，如 E:/wmx/uploadFiles/
        
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/**").addResourceLocations(
                        "classpath:/static/");
                registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                        "classpath:/META-INF/resources/");
                registry.addResourceHandler("/webjars/**").addResourceLocations(
                        "classpath:/META-INF/resources/webjars/");
                registry.addResourceHandler(resourceHandler).addResourceLocations("file:///" + location);
                super.addResourceHandlers(registry);
        }

        @Bean
        public ResponseBodyWrapFactoryBean getResponseBodyWrap(){
                return new ResponseBodyWrapFactoryBean();
        }
        
	@Bean
        public Docket createApi(){
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                    .apis(RequestHandlerSelectors.basePackage("com.huizhan.web.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo(){
            return new ApiInfoBuilder()
                    .title("API文档")
                    .description("API使用即参数定义")
                    .version("1.0.1")
                    .build();
        }
}